package com.SinaMicrobloggingAPI.Core.Process;

import com.SinaMicrobloggingAPI.Core.Request.*;
import com.SinaMicrobloggingAPI.Core.Response.*;
import com.SinaMicrobloggingAPI.Model.UserInfoModel;

public class LoggingProcess {
	public static UserInfoModel Logging(String userName, String password) {
		// 1.first to get request token
		String token = "";
		String tokenSecret = "";
		RequestTokenResponse requestTokenresponse = new RequestTokenResponse();
		RequestTokenRequest requestTokenRequest = new RequestTokenRequest(
				requestTokenresponse);
		requestTokenresponse = (RequestTokenResponse) requestTokenRequest
				.getResponse();
		token = requestTokenresponse.getOauthToken();
		tokenSecret = requestTokenresponse.getOauthTokenSecret();
		// 2.to get Authorize
		AuthorizeResponse authorizeResponse = new AuthorizeResponse();
		AuthorizeRequest authorizeRequest = new AuthorizeRequest(
				authorizeResponse);
		authorizeRequest.setUserId(userName);
		authorizeRequest.setPassword(password);
		authorizeRequest.setOauthToken(token);
		authorizeRequest.setOauthTokenSecret(tokenSecret);
		authorizeResponse = (AuthorizeResponse) authorizeRequest.getResponse();
		// 3. to get AccessToken
		AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
		AccessTokenRequest accessTokenRequest = new AccessTokenRequest(
				accessTokenResponse);
		accessTokenRequest.setOauthVerifier(authorizeResponse
				.getOauthVerifier());
		accessTokenRequest.setOauthToken(token);
		accessTokenRequest.setOauthTokenSecret(tokenSecret);

		accessTokenResponse = (AccessTokenResponse) accessTokenRequest
				.getResponse();
		token = accessTokenResponse.getOauthToken();
		tokenSecret = accessTokenResponse.getOauthTokenSecret();
		// confirm personal information
		VerifyCredentialsResponse verifyCredentialsResponse = new VerifyCredentialsResponse();
		VerifyCredentialsRequest verifyCredentialsRequest = new VerifyCredentialsRequest(
				verifyCredentialsResponse);
		verifyCredentialsRequest.setOauthToken(token);
		verifyCredentialsRequest.setOauthTokenSecret(tokenSecret);
		verifyCredentialsResponse = (VerifyCredentialsResponse) verifyCredentialsRequest
				.getResponse();
		UserInfoModel user = verifyCredentialsResponse.getUserInfo();
		user.setAccessToken(token);
		user.setAccessTokenSecret(tokenSecret);

		return user;
	}
}
